<?


JCAT_Package::ImportClass( ROOT_PATH.'Classes/Coder/class.CoderDocBase.php') ;

abstract class ViewDoc extends CoderDocBase implements IJCATMVCObject
{
	/**
	 * what's this
	 * 
	 * @access	public
	 * @param	$sPath=''	string	What's this Parameter ?
	 * @return	array
	 */
	public function ViewDoc($sName,$sPath='')
	{
		$this->CoderDocBase( $sName, $sPath ) ;
		
		// 初始化文档结构  :::::::::;
		
		// 子控件
		$aSubDoc = new UICtrlContainerDoc('UICtrlChildren') ;
		$this->AddDocument($aSubDoc,'UICtrlChildren') ;

		// 子视图
		$aSubDoc = new JCATMVCObjectContainerDoc('ViewChildren',JCATMVCObjectContainerDoc::OBJECT_TYPE_VIEW) ;
		JCAT_Document::RegisterGlobalName($aSubDoc,'Test') ;
		$this->AddDocument($aSubDoc,'ViewChildren') ;
	}

	/**
	 * what's this
	 * 
	 * @access	public
	 * @static 
	 * @return	string
	 */
	public function GetObjctName()
	{
		return $this->Get('ViewName') ;
	}
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @static 
	 * @return	string
	 */
	public function GetKindIcon($nSize=16)
	{
		return ROOT_PATH.'Resource/Image/View.24x24.png' ;
	}

	public function GetUICtrlChildren()
	{
		return $this->GetDocument('UICtrlChildren') ;
	}
	
	public function GetObjectChildrenDoc()
	{
		return $this->GetDocument('ViewChildren') ;
	}
	
	
	
	// 创建代码 /////////////////////////////////////////////
	
	
	/**
	 * 将序列化以后的内容存入到 文档
	 * 
	 * @access	protected
	 * @param	$sPath				文件路径
	 * @param	$sByteContents		字节内容
	 * @param	$bAsBinary			作为二进制存储
	 * @return	bool
	 */
	protected function SaveToFile( $sPath, $sByteContents, $bAsBinary=false )
	{
		// 调用父类
		parent::SaveToFile( $sPath, $sByteContents, $bAsBinary ) ;

		foreach ($this->arrCodeBlocks as $sCodeBlockName=>$nTabNum)
		{
			$sMethodName = 'CreateCode_'.str_replace(' ','',$sCodeBlockName) ;
			$this->WirteUnmodifiableBlock($sPath,$sCodeBlockName,$this->$sMethodName(),CoderDocBase::UnmodifiableBlockKind_Code,$nTabNum) ;
		}

		// 完成 返回 
		return true ;
	}


	public function CreateCode_ExtendsAndImplements()
	{
		$sRet = "	extends ".$this->GetObjectClass() ;
		if( $this->Get('ImplementsIViewForm') )
		{
			$sRet.= "\r\n	implements JCAT_IViewForm" ;
		}
		
		return $sRet ;
	}
	
	
	public function CreateCode_UICtrlChildren()
	{
		$aSubDoc = $this->GetUICtrlChildren() ;
		$arrUICtrls = $aSubDoc->Get('Children') ;
		
		if(!count($arrUICtrls))
		{
			return '' ;
		}
		
		$sTab = str_repeat("\t",2) ;
		$arrCodeLine[] = "{$sTab}// ".JCAT_Language::Sentence('载入UI控件') ;
		$arrCodeLine[] = "{$sTab}\$sCurrentDirectory = dirname(__FILE__).'/' ;" ;
		
		foreach ($arrUICtrls as $arrOneUICtrl)
		{
			$sUICtrlName = $arrOneUICtrl[TreeUICtrlChildren::CLM_NAME] ;
			$sUICtrlPath = $arrOneUICtrl[TreeUICtrlChildren::CLM_PATH] ;
			$sExchangeName = $arrOneUICtrl[TreeUICtrlChildren::CLM_EXCHANGE_TO] ;
			if($sExchangeName=='*')
			{
				$sExchangeName = $sUICtrlName ;
			}
			if($sExchangeName)
			{
				$sExchange = ",'{$sExchangeName}'" ;
			}
			else 
			{
				$sExchange = '' ;
			}
			
			// 实现方式：派生
			if( $arrOneUICtrl[TreeUICtrlChildren::CLM_IMPLEMENTS]===JCAT_Language::Sentence('子类化') )
			{
				$arrCodeLine[] = "{$sTab}JCAT_Package::ImportClass('{$sUICtrlName}',\$sCurrentDirectory.'{$sUICtrlPath}') ;" ;
				$arrCodeLine[] = "{$sTab}\$this->AddUICtrl(new {$sUICtrlName}(){$sExchange}) ;" ;
			}
			
			// 实现方式：实例
			else
			{
				$arrCodeLine[] = "{$sTab}\$this->AddUICtrl(JCAT_Package::ImportInstance(\$sCurrentDirectory.'{$sUICtrlPath}'){$sExchange}) ;" ;
			}
		}

		return implode("\r\n",$arrCodeLine) ;
	}

	public function CreateCode_ViewChildren()
	{
		$aSubDoc = $this->GetObjectChildrenDoc() ;
		$arrViews = $aSubDoc->Get('Children') ;
	
		if(!count($arrViews))
		{
			return '' ;
		}
		
		$sTab = str_repeat("\t",2) ;
		$arrCodeLine[] = "{$sTab}// ".JCAT_Language::Sentence('创建并添加子视图') ;
		$arrCodeLine[] = "{$sTab}\$sCurrentDirectory = dirname(__FILE__).'/' ;" ;
		
		foreach ($arrViews as $arrOneView)
		{
			$sSubViewName = $arrOneView[TreeJCATMVCObject::CLM_NAME] ;
			$sSubViewPath = $arrOneView[TreeJCATMVCObject::CLM_PATH] ;

			$arrCodeLine[] = "{$sTab}JCAT_Package::ImportClass('{$sSubViewName}',\$sCurrentDirectory.'{$sSubViewPath}') ;" ;
			$arrCodeLine[] = "{$sTab}\$this->AddView(new {$sSubViewName}(\$this)) ;" ;
		}

		return implode("\r\n",$arrCodeLine) ;
	}
	
	
	
	private $arrCodeBlocks = array(
				'Extends And Implements' => 1
				, 'Call Parent Construct' =>2
				, 'UICtrl Children' =>2
				, 'View Children' =>2
	) ;
}

JCAT_Package::ImportClass('JCAT_UIHtml',JCAT::PACKAGE_UIHTML) ;
?>